package cn.msqweb.controller;


import cn.msqweb.entity.ProdProp;
import cn.msqweb.entity.ProdPropValue;
import cn.msqweb.model.Result;
import cn.msqweb.service.ProdPropService;

import cn.msqweb.service.ProdPropValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格控制层
 */
@Api(tags = "商品规格接口管理")
@RequestMapping("prod/spec")
@RestController
public class ProdPropController {

    @Autowired
    private ProdPropService prodPropService;

    @Resource
    private ProdPropValueService prodPropValueService;


    @ApiOperation("多条件分页查询商品规格")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<Page<ProdProp>> loadProdSpecPage(@ApiParam("页码") @RequestParam Long current,
                                                   @ApiParam("每页显示条数") @RequestParam Long size,
                                                   @ApiParam("属性名称") @RequestParam(required = false) String propName) {
        Page<ProdProp> page = prodPropService.queryProdSpecPage(current,size,propName);
        return Result.success(page);
    }


    @ApiOperation("新增商品规格")
    @PostMapping
    @PreAuthorize("hasAuthority('prod:spec:save')")
    public Result<String> saveProdSpec(@ApiParam("商品属性对象") @RequestBody ProdProp prodProp) {
        Boolean saved = prodPropService.saveProdSpec(prodProp);
        return Result.handler(saved);
    }

    @ApiOperation("修改商品规格")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:spec:update')")
    public Result<String> modifyProdSpec(@ApiParam("商品属性对象") @RequestBody ProdProp prodProp) {
        Boolean modified = prodPropService.modifyProdSpec(prodProp);
        return Result.handler(modified);
    }

    @ApiOperation("删除商品规格")
    @DeleteMapping("{propId}")
    @PreAuthorize("hasAuthority('prod:spec:delete')")
    public Result<String> removeProdSpec(@ApiParam("商品属性标识") @PathVariable Long propId) {
        Boolean removed = prodPropService.removeProdSpec(propId);
        return Result.handler(removed);
    }

    @ApiOperation("查询商品属性集合")
    @GetMapping("list")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<List<ProdProp>> loadProdPropList() {
        List<ProdProp> list = prodPropService.list();
        return Result.success(list);
    }

    @ApiOperation("根据属性标识查询属性值集合")
    @GetMapping("listSpecValue/{propId}")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<List<ProdPropValue>> loadProdPropValues(@ApiParam("属性标识") @PathVariable Long propId) {
        List<ProdPropValue> list = prodPropValueService.list(new LambdaQueryWrapper<ProdPropValue>()
                .eq(ProdPropValue::getPropId, propId)
        );
        return Result.success(list);
    }
}
